package com.jzoom.zoom.admin.controllers;

import com.jzoom.zoom.admin.models.AccessService;
import com.jzoom.zoom.admin.models.ComponentsService;
import com.jzoom.zoom.common.annotations.Inject;
import com.jzoom.zoom.web.annotations.Controller;
import com.jzoom.zoom.web.annotations.JsonResponse;
import com.jzoom.zoom.web.annotations.Mapping;
import com.jzoom.zoom.web.exception.StatusException;

import java.util.HashMap;
import java.util.Map;

@Controller(key="login")
public class LoginController {

	@Inject
	AccessService accessService;

	@Inject
	private ComponentsService componentsService;


	@JsonResponse
	@Mapping(value="",method=Mapping.POST)
	public Map<String, Object> doLogin(String account,String pwd) throws StatusException.ApiError {
		Map<String, Object> data = new HashMap<String, Object>();
		String token = null;
		try {
			token = accessService.login(account, pwd);
			data.put("token", token);
			//js的最后更新时间
			data.put("version", componentsService.getLastUpdate());
			return data;
		} catch (AccessService.PasswordException e) {
			throw new StatusException.ApiError("登录失败");
		} catch (AccessService.UserNotFoundException e) {
			throw new StatusException.ApiError("登录失败");
		}

	}
	
}
